package com.atXiao.food.controller;


import com.atXiao.food.aspect.annotation.LogAspect;
import com.atXiao.food.constant.BusSignConstant;
import com.atXiao.food.constant.BuscodeConstant;
import com.atXiao.food.reqbody.ReqBody;
import com.atXiao.food.reqbody.category.CategoryVo;
import com.atXiao.food.rspbody.RspBody;
import com.atXiao.food.rspbody.category.CategoryItem;
import com.atXiao.food.rspcode.RspResultCode;
import com.atXiao.food.service.CategoryService;
import com.atXiao.food.util.OSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import com.atXiao.food.config.Log;

import javax.validation.Valid;
import java.io.File;
import java.util.List;

/**
 * @author: lixiaoxiao
 * @date: 2023/11/23 10:21
 * @version: 1.0
 * @desc: 分类列表Controller
 */
@RestController
@RequestMapping(path = "/category", produces = MediaType.APPLICATION_JSON_VALUE)
public class CategoryController {

    public final static Log logger = new Log(CategoryController.class);

    @Autowired
    CategoryService categoryService;

    @Autowired
    OSSUtil ossUtil;

    /**
     * 首页分类列表查询
     *
     * @param trace
     * @param reqBody
     * @return
     */
    @PostMapping(path = "/query")
    @ResponseBody
    @LogAspect(busSign = BusSignConstant.TASTE_FOOD, busCode = BuscodeConstant.CATEGORY_QUERY)
    public RspBody<List<CategoryItem>> queryCategoryList(@RequestHeader(name = "Trace") String trace, @Valid @RequestBody ReqBody<?> reqBody) {
        RspBody<List<CategoryItem>> rspBody = new RspBody();
        try {
            List<CategoryItem> categoryList = categoryService.getRecipeCategoryList();
            rspBody.setRetCode(RspResultCode.REQUEST_SUCCESS.getCode());
            rspBody.setRetDesc(RspResultCode.REQUEST_SUCCESS.getDesc());
            rspBody.setRspBody(categoryList);
        } catch (Exception e) {
            logger.error(trace, BusSignConstant.TASTE_FOOD, BuscodeConstant.CATEGORY_QUERY, e.getMessage(), e);
            rspBody = RspBody.fail(null);
        }
        return rspBody;
    }


    /**
     * 增加分类列表
     *
     * @param trace
     * @param reqBody
     * @return
     */
    @PostMapping(path = "/add")
    @ResponseBody
    @LogAspect(busSign = BusSignConstant.TASTE_FOOD, busCode = BuscodeConstant.ADD_CATEGORY_ITEM)
    public RspBody<List<CategoryItem>> addCategoryItem(@RequestHeader(name = "Trace") String trace, @Valid @RequestBody ReqBody<CategoryVo> reqBody) {
        RspBody rspBody = new RspBody();
        try {
            rspBody = categoryService.addCategory(trace, reqBody.getReqBody());
        } catch (Exception e) {
            logger.error(trace, BusSignConstant.TASTE_FOOD, BuscodeConstant.ADD_CATEGORY_ITEM, e.getMessage(), e);
            rspBody = RspBody.fail(null);
        }
        return rspBody;
    }




}
